package com.zidian.study.globa;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.zidian.student.trucks.R;

/**
 * Created by Administrator on 2016/8/23 0023.
 */
public abstract class ToolBarActivity extends  MySwipeBackActivity {
    private ToolBarHelper mToolBarHelper;
    public Toolbar toolbar;
    protected Context context;
    public ImageButton backBtn;
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            Window window = getWindow();
//            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
//            window.setFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS, WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//            window.setStatusBarColor(Color.TRANSPARENT);
////            window.setNavigationBarColor(ContextCompat.getColor(this, R.color.black));
//            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//            ViewGroup mContentView = (ViewGroup) ToolBarActivity.this.findViewById(Window.ID_ANDROID_CONTENT);
//            View mChildView = mContentView.getChildAt(0);
//            if (mChildView != null) {
//                //注意不是设置 ContentView 的 FitsSystemWindows, 而是设置 ContentView 的第一个子 View . 使其不为系统 View 预留空间.
//                ViewCompat.setFitsSystemWindows(mChildView, false);
//            }
//        }
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        //记录每个activity 方便管理activity
        ActManager.getAppManager().addActivity(this);
        context = this;

    }

    /**
     * 设置每个toolbar的title
     */
    protected void setTitle(String title) {
        mToolBarHelper.setTitle(title);
    }

    /**
     * toolbar的id
     */
    public static int toolbarView = 0;

    /**
     * 设置自定义的toolbar  如果不调用 使用默认的toolbar
     */
    public void setToolbarView(int toolbarView) {
        this.toolbarView = toolbarView;
        if (mToolBarHelper != null) {
            setSupportActionBar(toolbar);
        }
    }


    @Override
    public void setContentView(int layoutResID) {
        mToolBarHelper = new ToolBarHelper(this, layoutResID, toolbarView);
        toolbar = mToolBarHelper.getToolBar();
        setContentView(mToolBarHelper.getContentView());
        /*把 toolbar 设置到Activity 中*/
        setSupportActionBar(toolbar);
        /*自定义的一些操作*/
        onCreateCustomToolBar(toolbar);
    }

    /**
     * 自定一toolbar上的各种操作
     *
     * @param toolbar
     */
    public void onCreateCustomToolBar(Toolbar toolbar) {
        toolbar.setContentInsetsRelative(0, 0);
        if (toolbar.findViewById(R.id.toolbar_back) != null) {
            backBtn = (ImageButton) toolbar.findViewById(R.id.toolbar_back);
            backBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    closeAct();
                }
            });
        }

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    /**
     * 关闭当前的Activity
     */
    protected void closeAct() {
//        ActManager.getAppManager().finishActivity();
        finish();
    }


    /**
     * 跳转Activity
     */
    protected void startActivity(Class<?> cls) {
        Intent intent = new Intent(this, cls);
        startActivity(intent);    //这里用getActivity().startActivity(intent);
    }
}
